<template>
    <view class="container" v-if="sectionDatail">
        <video-component :data="videoData" v-if="videoData&&sectionDatail.contentType==1"></video-component>
        <audio-component id='audio' :receiveData="audioData" v-if="audioData&&sectionDatail.contentType==3"></audio-component>
        <view class="section_detail">
            <view class="section_base_data">
                <text class="section_title">{{sectionDatail.title}}</text>
                <view class="section_base_bottom">
                    <text class="class_num" v-if="sectionDatail.contentType==1">视频</text>
                    <text class="class_num" v-if="sectionDatail.contentType==2">直播</text>
                    <text class="class_num" v-if="sectionDatail.contentType==3">音频</text>
                    <text class="class_num" v-if="sectionDatail.contentType==4">图文</text>
                    <text class="dot">·</text>
                    <text class="update_time">{{sectionDatail.endTime}}更新</text>
                </view>
                <view class="belong_course_block" :data-url="'course_detail.html?instanceId='+sectionDatail.belongCourseInstanceId" @tap="tolinkUrl">
                    <view class="belong_course_left">
                        <view class="belong_course_info">
                            <text class="belong_course_text">来自</text>
                            <text class="belong_course_title">{{sectionDatail.belongCourseTitle}}</text>
                        </view>
                    </view>
                    <image class="belong_course_icon" src="https://image1.sansancloud.com/xianhua/2020_1/2/18/38/31_120.jpg?x-oss-process=style/preview_120"></image>
                </view>
                <text class="section_description">
                    {{sectionDatail.description}}
                </text>
            </view>
            <view class="section_blcok" v-if="sectionDatail.contentType==4">
                <view class="section_rich">
                    <rich-text-component :receiveData="articleJsonData" v-if="articleJsonData"></rich-text-component>
                </view>
            </view>
        </view>
        <!-- <view class="section_btn_list" v-if="sectionDatail.userPayed==0">
            <view class="add_section_btn" :style="{background:defaultColor}">
                <text class="add_section_btn_text">加入课程</text>
                <text class="add_section_price">￥{{sectionDatail.sectionPrice}}</text>
            </view>
        </view> -->
    </view>
</template>

<script>
import { socketFun } from "../../wxcomponents/public/json2Form";
export default {
  data() {
    return {
      sectionDatail:null,
      secondColor:'',
      defaultColor:'',
      params:{},
      articleJsonData:'',
      videoData:'',
      audioData:'',
      setting:null,
    };
  },
  props: {},

  /*onload*/
  onLoad: function (options) {
    let that=this;
    console.warn("======onLoad:options======", options, getApp().globalData.setting);
    that.params=options;
    that.getSectionData(options);
  },
  onReady: function () {
    
  },
  onHide: function () {
    console.log("=====onhide=====")
  },
  onUnload: function () {
      console.log("=====onUnload=====");
      try{
          this.selectComponent("#audio").stopAudioFun();
      }catch(e){
          //TODO handle the exception
      }
  },
  onShow: function () {
    let that=this;
	that.setData({
        setting:getApp().globalData.setting,
        defaultColor:getApp().globalData.setting.platformSetting.defaultColor,
        secondColor:getApp().globalData.setting.platformSetting.secondColor,
       })
  },
  /* 分享 app.js862行*/
  onShareAppMessage: function () {
    console.log(getApp().globalData.miniIndexPage);
    return getApp().globalData.shareForFx2(getApp().globalData.miniIndexPage);
  },
  onPullDownRefresh: function () {
    let that = this; // 下拉刷新的时候首先判断存不存在tab
    console.log("下拉");
    that.getsectionData(that.params);
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('===onReachBottom====');
    let that = this
  },
  onPageScroll: function (object) {
      let that=this;
      // that.setData({
      //   scroll: object.scrollTop
      // })
  },
  methods: {
    /* 获取数据 */
    getSectionData: function (params) {
      var that = this
      var customIndex = getApp().globalData.AddClientUrl("/wx_get_course_section_detail.html", params)
      getApp().globalData.showToastLoading('loading', true)
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          wx.hideLoading()
          console.log(res.data)
          let data=res.data.relateObj
          if (!data ) {
            that.setData({ sectionDatail: null })
          } else {
             data.endTime=data.endTime.slice(0,11)
             data.startTime=data.startTime.slice(0,11)
             let articleJsonData='';
             if (data.content) {
                articleJsonData=data.content;
             }else if(data.section&&data.section.content){
                 articleJsonData=data.section.content||''
             }
             console.log('====articleJsonData====', articleJsonData);
            that.setData({
              articleJsonData: {jsonData:{content:articleJsonData}}
            });
            if(data.contentType==1){
                that.setData({
                  videoData: {jsonData:{source:data.streamMedia,autoPlay:"0"}}
                });
            }
            if(data.contentType==3){
                that.setData({
                  audioData: {jsonData:{source:data.streamMedia,autoPlay:"0",posterImg:data.banner||'',name:data.title,author:getApp().globalData.clientNo,}}
                });
            }
            console.log("videoData",that.videoData,that.audioData)
            that.setData({ sectionDatail: data })
          }
          wx.hideLoading()
        },
        fail: function (res) {
          console.log("fail")
          wx.hideLoading()
          getApp().globalData.loadFail()
        }
      })
    },
    /* 组件事件集合 */
    tolinkUrl: function (e) {
      console.warn("=======e=======", e);
      let linkUrl = e.currentTarget.dataset.url;
      getApp().globalData.linkEvent(linkUrl);
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>